<!--商品货源-->
<template>
  <div>


    <div class="font-background">

      <img src="../../assets/page2/fontback5.png" alt="">
      <img src="../../assets/page2/fontback6.png" alt="" style="margin-left: 20px;">

      <div style="color:#FCFCFC;font-family: 'PingFang SC';font-size: 80px;margin-top: 10px">农资农科新时代</div>
    </div>

    <Subtitle subtitle="商品列表" description="买到就是赚到哦~" style="margin-top:100px;" />

    <div class="home-goods">
      <goods-sourse v-if="goods.length > 0" :cgoods="goods" @handleSearch="handleSearch" />
      <div v-else style="padding: 60px 0; color: #999; font-size: 20px; text-align: center;">
        暂无商品
      </div>
      <div class="pagination-wrapper">
        <el-pagination layout="prev, pager, next" :total="total" :page-size="pageSize" :current-page="pageNum"
          @current-change="pageClick" />
      </div>
    </div>
  </div>

</template>

<script>
import { selectAllPage, selectGoodsPage } from "../../api/order";
import GoodsSourse from "./GoodsSource.vue";
import Pagination from "../../components/Pagination";
import Subtitle from "../../components/Subtitle";

export default {
  data() {
    return {
      goods: [],
      total: 0,
      pageNum: 1,
      pageSize: 12,
      searchText: ''
    };
  },
  mounted() {
    this.$store.commit("updateActiveIndex", "2");
    this.getData()
  },
  methods: {
    getData() {
      selectAllPage({
        pageNum: this.pageNum,
        keys: this.searchText,
      }).then((res) => {
        if (res.code === 20000) {
          this.goods = res.data.list;
          this.total = res.data.total;
          this.pageSize = res.data.pageSize;
        }
      });
    },
    pageClick(pageNum) {
      this.pageNum = pageNum; // 更新当前页码
      this.getData();         // 重新获取数据
    },
    handleSearch(searchText) {
      this.searchText = searchText;
      selectGoodsPage({
        pageNum: this.pageNum,
        keys: this.searchText,
      }).then((res) => {
        if (res.code === 20000) {
          this.goods = res.data.list;
          this.total = res.data.total;
          this.pageSize = res.data.pageSize;
        }
      });
    }
  },
  components: {
    Pagination,
    GoodsSourse,
    Subtitle
  },
  // 监听,当路由发生变化的时候执行
  watch: {
    $route: {
      handler: function (val, oldVal) {
        console.log(val);
      },
      // 深度观察监听
      deep: true
    }
  },
};
</script>

<style lang="less" scoped>
.home-goods {
  width: 1100px;
  margin: 0 auto;
  display: flex;
  flex-direction: column;
  align-items: center;
}

.font-background {
  background-image: url("../../assets/page2/background2.jpg");
  height: 400px;
  width: 1920px;
  padding-left: 350px;
  padding-top: 90px;
}

.pagination-wrapper {
  margin-top: 32px;
  display: flex;
  justify-content: center;
  width: 100%;
}
</style>
